<?php
class GM
{
    public static function SubstringTH($str,$start,$end)
    {
        if( strlen($str) == $end || strlen($str) <= $end-3 ){
            return $str;
        }
        else {
            return iconv_substr($str, $start, $end).'...';
        }
    }
    
    private static function getnameMonthTH($numMonth)
    {
        switch($numMonth)
        {
            case 1 : return 'มกราคม';
            case 2 : return 'กุมภาพันธ์';
            case 3 : return 'มีนาคม';
            case 4 : return 'เมษายน';
            case 5 : return 'พฤษภาคม';
            case 6 : return 'มิถุนายน';
            case 7 : return 'กรกฎาคม';
            case 8 : return 'สิงหาคม';
            case 9 : return 'กันยายน';
            case 10 : return 'ตุลาคม';
            case 11 : return 'พฤศจิกายน';
            case 12 : return 'ธันวาคม';
            default : return $numMonth;
        }
    }
    
    private static function getnameMonthEN($numMonth)
    {
        switch($numMonth)
        {
            case 1 :  return 'January';
            case 2 :  return 'February';
            case 3 :  return 'March';
            case 4 :  return 'April';
            case 5 :  return 'May';
            case 6 :  return 'June';
            case 7 :  return 'July';
            case 8 :  return 'August';
            case 9 :  return 'September';
            case 10 : return 'October';
            case 11 : return 'November';
            case 12 : return 'December';
            default : return $numMonth;
        }
    }
    
    Public static function setformDateTH($str)
    {
        $from=explode('-', $str);
        $month = self::getnameMonthTH($from[1]);
        $from[2] = ($from[2] < 10)?substr($from[2],1,1):$from[2];
        return $from[2].' '.$month.' '.($from[0]+543);
    }
    
    Public static function setformDateEN($str)
    {
        $from=explode('-', $str);
        $month = self::getnameMonthEN($from[1]);
        $from[2] = ($from[2] < 10)?substr($from[2],1,1):$from[2];
        return $from[2].' '.$month.' '.$from[0];
    }
    
    public static function getProvince($lang){
        if($lang == 'th'){
            $sql="SELECT *, ProvinceNameTH AS 'ProvinceName' FROM province ORDER BY ProvinceNameTH;";
        }else{
            $sql="SELECT *, ProvinceNameEN AS 'ProvinceName' FROM province ORDER BY ProvinceNameEN;";
        }
        $connection=Yii::app()->db; 
        $command=$connection->createCommand($sql);
        return $command->queryAll();
    }
    
    public static function getOptionDropdownProvince($lang){
        if($lang == 'th'){
            $sql="SELECT *, ProvinceNameTH AS 'ProvinceName' FROM province ORDER BY ProvinceNameTH;";
        }else{
            $sql="SELECT *, ProvinceNameEN AS 'ProvinceName' FROM province ORDER BY ProvinceNameEN;";
        }
        $connection=Yii::app()->db; 
        $command=$connection->createCommand($sql);
        $province = $command->queryAll();
        $dropdown = "";
        foreach ($province as $value){
            $dropdown .= '<option value="'.$value['ProvinceID'].'">'.$value['ProvinceName'].'</option>';
        }
        return $dropdown;
    }
    
    public static function findYear($date1, $date2){
//        $date1 = "2007-03-24";
//        $date2 = "2009-06-26";

        $diff = abs(strtotime($date2) - strtotime($date1));

        return floor($diff / (365 * 60 * 60 * 24));
    }
    
    public static function paging($url, $page, $maxPage){
        echo ''?>
        <div class="pagination pagination-right">
        <ul>
        <?php if($maxPage!=0){ ?>
            <li class="<?=$page>1?'':'disabled'?>"><a href="<?= $page>1?$url.'p='.($page-1):'#' ?>">Previous</a></li>
            <?php for($i=1;$i<=$maxPage;$i++){ ?>
            <li class="<?= ($i==$page)?'active':'' ?>"><a href="<?= $url ?>p=<?= $i ?>"><?= $i ?></a></li>
            <?php } ?>
            <li class="<?=$page!=$maxPage?'':'disabled'?>"><a href="<?= $page!=$maxPage?$url.'p='.($page+1):'#' ?>">Next</a></li>
        <?php } ?>
        </ul>
        </div> <?php ;
    }
    
    public static function DateDiff($strDate1,$strDate2) {
        return (strtotime($strDate2) - strtotime($strDate1))/  ( 60 * 60 * 24 );  // 1 day = 60*60*24
    }
    
    public static function GetLabelsNew(){
        return "<span class=\"label label-warning\">NEW !</span> ";
    }
    
    public static function sendEMail($sender, $fromname, $subject, $body, $receiver) {
        require_once("PHPMailer/class.phpmailer.php");
        $mail = new PHPMailer();
        $mail->CharSet = "utf-8";
        $mail->IsSMTP();
        $mail->Host = 'mail.avance-thai.com';
        $mail->Port = 25;
        $mail->SMTPAuth = true;
        $mail->Username = 'system@avance-thai.com ';
        $mail->Password = 'sysavance';
        $mail->From = $sender;
        $mail->FromName = $fromname;
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AltBody = $body;
        $mail->AddAddress($receiver);
        return $mail->Send();
    }
    
    public static function genUniqKey($word = ""){
        return uniqid($word, true);
    }
}
?>
